<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>手写bind函数实现</title>
</head>
<body>
  <script>
    function foo(name, age) {
      console.log(this, name, age)
    }
    
    Function.prototype.hybind = function(thisArg, ...otherArgs) {

      thisArg = (thisArg === null || thisArg === undefined) ? window : Object(thisArg)
      
      Object.defineProperty(thisArg, "fn", {
        enumerable: false,
        configurable: false,
        writable: false,
        value: this
      })

      return (...newArgs) => {
        
        var allArgs = [...newArgs, ...otherArgs]
        thisArg.fn(...allArgs)
      }
    }
  </script>
</body>
</html>